package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SetStepCode;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.ISetStepCodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 步骤类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-21
 */
@Service
public class SetStepCodeServiceImpl implements ISetStepCodeService
{
    private static final Logger log = LoggerFactory.getLogger(SetStepCodeServiceImpl.class);

    @Autowired
    private SetStepCodeMapper setStepCodeMapper;


    /**
     * 查询步骤类型
     * 
     * @param id 步骤类型主键
     * @return 步骤类型
     */
    @Override
    public SetStepCode selectSetStepCodeById(Long id)
    {
        return setStepCodeMapper.selectSetStepCodeById(id);
    }
    

    /**
     * 查询步骤类型列表
     * 
     * @param setStepCode 步骤类型
     * @return 步骤类型
     */
    @Override
    public List<SetStepCode> selectSetStepCodeList(SetStepCode setStepCode)
    {
        return setStepCodeMapper.selectSetStepCodeList(setStepCode);
    }

    /**
     * 新增步骤类型
     * 
     * @param setStepCode 步骤类型
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSetStepCode(SetStepCode setStepCode)
    {
        setStepCode.setCreateTime(DateUtils.getNowDate());
        setStepCode.setCreateBy(SecurityUtils.getUsername());
        setStepCode.setStatus(UserConstants.NORMAL);
        return setStepCodeMapper.insertSetStepCode(setStepCode);
    }

    /**
     * 修改步骤类型
     * 
     * @param setStepCode 步骤类型
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSetStepCode(SetStepCode setStepCode)
    {
        setStepCode.setUpdateTime(DateUtils.getNowDate());
        setStepCode.setUpdateBy(SecurityUtils.getUsername());
        return setStepCodeMapper.updateSetStepCode(setStepCode);
    }

    /**
     * 批量删除步骤类型
     *
     * @param ids 需要删除的步骤类型主键
     * @return 结果
     */
    @Override
    public int deleteSetStepCodeByIds(Long[] ids) {
        return setStepCodeMapper.deleteSetStepCodeByIds(ids);
    }

    /**
     * 删除步骤类型信息
     * 
     * @param id 步骤类型主键
     * @return 结果
     */
    @Override
    public int deleteSetStepCodeById(Long id)
    {
        return setStepCodeMapper.deleteSetStepCodeById(id);
    }

    /**
     * 导入步骤类型数据
     *
     * @param stepCodeList 步骤类型数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importStepCode(List<SetStepCode> stepCodeList, boolean isUpdateSupport, String operName) {

        if (StringUtils.isNull(stepCodeList) || stepCodeList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //String password = configService.selectConfigByKey("sys.user.initPassword");
        for (SetStepCode setStepCode : stepCodeList)
        {
            try
            {
                // 验证是否存在这个步骤类型
                List<SetStepCode> ssc = setStepCodeMapper.selectSetStepCodeList(setStepCode);

                if (StringUtils.isNull(ssc))
                {
                    setStepCode.setCreateBy(operName);
                    this.insertSetStepCode(setStepCode);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、步骤类型 " + setStepCode.getStepName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    setStepCode.setUpdateBy(operName);
                    this.updateSetStepCode(setStepCode);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、步骤类型 " + setStepCode.getStepName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、步骤类型 " + setStepCode.getStepName() + " 已存在");
                }

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、步骤类型 " + setStepCode.getStepName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 校验步骤类型名称是否唯一
     *
     * @param setStepCode 步骤类型信息
     * @return 结果
     */
    @Override
    public String checkNameUnique(SetStepCode setStepCode)
    {
        Long id = StringUtils.isNull(setStepCode.getId()) ? -1L : setStepCode.getId();
        SetStepCode info = setStepCodeMapper.checkNameUnique(setStepCode.getStepName());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
